package protocol

import (
	"errors"
	"fmt"
)

type Protocol struct {
	Header Header
	Body Body
}

type Header struct {
	ContentLength uint16
	Type byte
}

type Body []byte

func (p *Protocol) ToBytes() []byte {
 	var  buf []byte
	p.Header.ContentLength = uint16(len(p.Body))
	buf = append(buf, p.Header.Type)
	buf = append(buf,[]byte{byte(p.Header.ContentLength),byte(p.Header.ContentLength>>8)}...)
	fmt.Println(len(buf))
	buf = append(buf,p.Body...)
	return buf
}

func BytesToUint16(bs []byte) (uint16,error){
	if (len(bs) < 2) {
		return 0,errors.New("error")
	}
	return  uint16(bs[0])+uint16(bs[1] << 8),nil
}
